package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.fremework.BaseController;
import cn.stylefeng.guns.fremework.ResponseData;
import cn.stylefeng.guns.modular.system.entity.SysDictGroup;
import cn.stylefeng.guns.modular.system.entity.SysDictItem;
import cn.stylefeng.guns.modular.system.service.SysDictGroupService;
import cn.stylefeng.guns.modular.system.service.SysDictItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
@RequestMapping("/sysDict")
public class SysDictController extends BaseController {


    @Autowired
    private SysDictGroupService sysDictGroupService;

    @Autowired
    private SysDictItemService sysDictItemService;


    private String PREFIX = "/modular/system/sysDict";


    @RequestMapping("")
    public String index() {
        return PREFIX + "/sysDict.html";
    }

//////////////////////////////////////////字典组接口///////////////////////////////////
    ///字典组列表
    @ResponseBody
    @GetMapping("/getDictGroupPage")
    public ResponseData getDictGroupPage(Page page, SysDictGroup params) {
        LambdaQueryWrapper<SysDictGroup> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(params.getGroupName())){
            queryWrapper.and(wrapper -> wrapper.like(SysDictGroup::getGroupName, params.getGroupName()));
        }
        IPage result = sysDictGroupService.page(page, queryWrapper);
        return ResponseData.success(result);
    }
    ///字典组添加
    @RequestMapping("/sysDictGroupAddPage")
    public String sysDictGroupAddPage() {
        return PREFIX + "/sysDictGroupAdd.html";
    }

    @ResponseBody
    @PostMapping("/sysDictGroupSave")
    public ResponseData sysDictGroupSave(SysDictGroup params) {
        this.sysDictGroupService.save(params);
        return ResponseData.success();
    }
    ///字典组详情
    @ResponseBody
    @RequestMapping(value = "/getSysDictGroupDetail/{groupId}")
    public ResponseData getSysDictGroupDetail(@PathVariable Long groupId) {
        SysDictGroup result = this.sysDictGroupService.getById(groupId);
        return ResponseData.success(result);
    }
    ///字典组修改
    @RequestMapping("/sysDictGroupEditPage")
    public ModelAndView sysDictGroupEditPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(PREFIX + "/sysDictGroupEdit.html");
        return modelAndView;
    }
    @ResponseBody
    @PostMapping("/sysDictGroupUpdate")
    public ResponseData sysDictGroupUpdate(SysDictGroup params) {
        this.sysDictGroupService.updateById(params);
        return ResponseData.success();
    }
    ///字典组删除
    @ResponseBody
    @PostMapping("/sysDictGroupRemove/{groupId}")
    public ResponseData sysDictGroupRemove(@PathVariable Long groupId) {
        this.sysDictGroupService.removeById(groupId);
        return ResponseData.success();
    }
    //////////////////////////////////////////FIXME 字典项接口
    @ResponseBody
    @GetMapping("/getDictItemPage")
    public ResponseData getDictItemPage(Page page, SysDictItem params) {
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(params.getGroupCode())){
            queryWrapper.and(wrapper -> wrapper.eq(SysDictItem::getGroupCode, params.getGroupCode()));
        }
        if (!StringUtils.isEmpty(params.getItemValue())){
            queryWrapper.and(wrapper -> wrapper.like(SysDictItem::getItemValue, params.getItemValue()));
        }
        IPage result = sysDictItemService.page(page, queryWrapper);
        return ResponseData.success(result);
    }
    ///字典项添加
    @RequestMapping("/sysDictItemAddPage")
    public String sysDictItemAddPage() {
        return PREFIX + "/sysDictItemAdd.html";
    }

    @ResponseBody
    @PostMapping("/sysDictItemSave")
    public ResponseData sysDictItemSave(SysDictItem params) {
        sysDictItemService.save(params);
        return ResponseData.success();
    }
    ///字典项详情
    @ResponseBody
    @RequestMapping(value = "/getSysDictItemDetail/{itemId}")
    public ResponseData getSysDictItemDetail(@PathVariable Long itemId) {
        SysDictItem result = sysDictItemService.getById(itemId);
        return ResponseData.success(result);
    }
    ///字典项修改
    @RequestMapping("/sysDictItemEditPage")
    public ModelAndView sysDictItemEditPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(PREFIX + "/sysDictItemEdit.html");
        return modelAndView;
    }
    @ResponseBody
    @PostMapping("/sysDictItemUpdate")
    public ResponseData sysDictItemUpdate(SysDictItem params) {
        sysDictItemService.updateById(params);
        return ResponseData.success();
    }
    @ResponseBody
    @PostMapping("/sysDictItemRemove/{itemId}")
    public ResponseData sysDictItemRemove(@PathVariable Long itemId) {
        sysDictItemService.removeById(itemId);
        return ResponseData.success();
    }

    @ResponseBody
    @PostMapping("/sysDictItemListByGroupCode/{groupCode}")
    public List<SysDictItem> sysDictItemListByGroupCode(@PathVariable String groupCode) {
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(groupCode)){
            queryWrapper.and(wrapper -> wrapper.eq(SysDictItem::getGroupCode, groupCode));
        }
        List<SysDictItem> result = sysDictItemService.list(queryWrapper);
        return result;
    }

}


